#!/bin/sh -efu
### This file is covered by the GNU General Public License,
### which should be included with libshell as the file LICENSE.
### All copyright information are listed in the COPYING.

if [ -z "${__included_shell_args-}" ]; then
__included_shell_args=1

. shell-error
. shell-var

### Checks that given option value is a readable file.
### Arguments: $1 is option name, $2 is option value.
### If $2 is a readable file, outputs canonicalized file name,
### otherwise fails.
opt_check_read()
{
	local value
	[ -r "$2" ] &&
	value="$(readlink -fv -- "$2")" ||
		fatal "$1: $2: file not available."
	printf %s "$value"
}

### Checks that given option value is an executable file.
### Arguments: $1 is option name, $2 is option value.
### If $2 is a executable file, outputs canonicalized file name,
### otherwise fails.
opt_check_exec()
{
	local value
	[ -f "$2" ] && [ -x "$2" ] &&
	value="$(readlink -fv -- "$2")" ||
		fatal "$1: $2: file not executable."
	printf %s "$value"
}

### Checks that given option value is a traversable directory.
### Arguments: $1 is option name, $2 is option value.
### If $2 is a readable file, outputs canonicalized directory name,
### otherwise fails.
opt_check_dir()
{
	local value
	[ -d "$2" ] && [ -x "$2" ] &&
	value="$(readlink -fv -- "$2")" ||
		fatal "$1: $2: directory not available."
	printf %s "$value"
}

### Checks that given option value is a positive decimal number.
### Arguments: $1 is option name, $2 is option value.
### If $2 is a positive decimal number, outputs it, otherwise fails.
opt_check_number()
{
	shell_var_is_number "$2" ||
		fatal "$1: $2: invalid number."
	printf %s "$2"
}

show_usage()
{
	[ -z "$*" ] || message "$*"
	echo "Try \`$PROG --help' for more information." >&2
	exit 1
}

show_help()
{
	fatal "show_help(): Undefined function"
}

print_version()
{
	fatal "print_version() Undefined function"
}

readonly getopt_common_opts="q,v,V,h"
readonly getopt_common_longopts="quiet,verbose,version,help"
parse_common_option()
{
	case "$1" in
		-h|--help) show_help
			;;
		-q|--quiet) quiet=-q; verbose=
			;;
		-v|--verbose) verbose=-v; quiet=
			;;
		-V|--version) print_version "$PROG"
			;;
		*) fatal "Unrecognized option: $1"
			;;
	esac
}

fi #__included_shell_args
